package com.btex.app.tools;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.alibaba.security.rp.RPSDK;
import com.btex.app.AuthenticationInformationAcitvity;
import com.btex.app.CertificationOneActivity;
import com.btex.app.Config;
import com.btex.app.R;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.PreferencesCookieStore;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author zhangyi<zhangyi, 1104745049@qq.com>
 * @version v1.0
 * @project study1
 * @Description
 * @encoding UTF-8
 * @date 2018/8/13
 * @time 下午4:48
 * @修改记录 <pre>
 * 版本       修改人         修改时间         修改内容描述
 * --------------------------------------------------
 * <p>
 * --------------------------------------------------
 * </pre>
 */


public class OTCRealNameDialog extends Dialog {
    private final Context context;
    private String mProve_level;
    @BindView(R.id.otc_dialog_diss)
    TextView mOtcDialogDiss;
    @BindView(R.id.otc_dialog_set)
    TextView mOtcDialogSet;
    private MicroAVLIDialog mMicroAVLIDialog;


    public OTCRealNameDialog(Context context, String prove_level) {
        super(context, R.style.coupon_style);
        this.context = context;
        mProve_level = prove_level;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.otc_real_name_dialog);
        ButterKnife.bind(this);

        mMicroAVLIDialog = new MicroAVLIDialog(context);
        //获取屏幕宽高
        DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
        //屏幕宽
        int widthPixels = displayMetrics.widthPixels;
        //屏幕高
        int heightPixels = displayMetrics.heightPixels;
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        //设置给attributes
        attributes.width = (int) (widthPixels * 0.9);
        attributes.height = (int) (heightPixels * 0.25);
        //让Dialog位置居中
        attributes.gravity = Gravity.CENTER;
        getWindow().setAttributes(attributes);
        //点击空白处不dismiss
        setCanceledOnTouchOutside(true);


    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        return super.onKeyDown(keyCode, event);
    }

    @OnClick({R.id.otc_dialog_diss, R.id.otc_dialog_set})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.otc_dialog_diss:
                dismiss();
                break;
            case R.id.otc_dialog_set:


                goRenZhen();
//                goCertification();
                break;
        }
    }

    private void goRenZhen() {
        context.startActivity(new Intent(context, AuthenticationInformationAcitvity.class));
        dismiss();
    }

    private void goCertification() {
        if (mProve_level.equals("C0")) {

            context.startActivity(new Intent(context, CertificationOneActivity.class));
            dismiss();

        }

        if (mProve_level.equals("C1")) {

            getData();


        }
    }

    private void getData() {
        mMicroAVLIDialog.show();
        SharedPreferencesUtil sp = SharedPreferencesUtil.getSharedPreferences(context);
        PreferencesCookieStore cookieStore = CookieTool.getCookieStore(context, sp);
        int num = (int) ((Math.random() * 9 + 1) * 100000);
        RequestParams params = new RequestParams();
        params.addHeader("myclient", "android");
        new HttpUtils().configCookieStore(cookieStore).send(HttpRequest.HttpMethod.GET, Config.GET_CHECKRP_TOKEN + "?rnd=" + String.valueOf(num),params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                mMicroAVLIDialog.dismiss();
                String result = responseInfo.result;

                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if ("1".equals(jsonObject.getString(Config.KEY_CODE))) {
                        String data = jsonObject.getString("data");
                        toAliyun(data);
                        dismiss();

                    } else {
//                        Snackbar snackbar = Util.ShortSnackbar(mOtcDialogSet, jsonObject.getString("info"), Color.parseColor("#ffffff"), Color.parseColor("#272736"));
//                        snackbar.show();
                        Util.showSnachBarDialog(mOtcDialogDiss, context.getResources().getColor(R.color.red), jsonObject.getString("info"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();

                }

            }

            @Override
            public void onFailure(HttpException error, String msg) {


            }
        });
    }


    private void toAliyun(String token) {

        Log.i("lkmdqwdoqiwdjoq", token);

        RPSDK.start(token, context,
                new RPSDK.RPCompletedListener() {
                    @Override
                    public void onAuditResult(RPSDK.AUDIT audit) {


                        if (audit == RPSDK.AUDIT.AUDIT_PASS) {
                            //认证通过
                            Log.i("kjnqwjdqwuidh", "sucess");

                        } else if (audit == RPSDK.AUDIT.AUDIT_FAIL) {
                            //认证不通过
                            Log.i("kjnqwjdqwuidh", "1");

                        } else if (audit == RPSDK.AUDIT.AUDIT_IN_AUDIT) {
                            //认证中，通常不会出现，只有在认证审核系统内部出现超时，未在限定时间内返回认证结果时出现。此时提示用户系统处理中，稍后查看认证结果即可。

                        } else if (audit == RPSDK.AUDIT.AUDIT_NOT) {
                            //未认证

                            Log.i("kjnqwjdqwuidh", "3");

                        } else if (audit == RPSDK.AUDIT.AUDIT_EXCEPTION) {
                            //系统异常
//                            Snackbar snackbar = Util.ShortSnackbar(mOtcDialogDiss, "系统异常", Color.parseColor("#ffffff"), Color.parseColor("#272736"));
//                            snackbar.show();
                            Util.showSnachBarDialog(mOtcDialogDiss, context.getResources().getColor(R.color.red), "系统异常");
                        }
                    }
                });
    }


}
